<template>
  <div class="register-container">
    <div class="register-box">
      <div class="register-header">
        <div class="logo">
          <i class="el-icon-s-platform"></i>
        </div>
        <h1 class="register-title">零工用户注册</h1>
        <p class="register-subtitle">快速注册，开启灵活就业之旅</p>
      </div>

      <el-form :model="registerForm" :rules="rules" ref="registerFormRef" label-width="100px" class="register-form">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="registerForm.name" placeholder="请输入真实姓名" class="custom-input"></el-input>
        </el-form-item>

        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="registerForm.phone" placeholder="请输入手机号码" class="custom-input"></el-input>
        </el-form-item>

        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="registerForm.idCard" placeholder="请输入18位身份证号码" class="custom-input"></el-input>
        </el-form-item>

        <el-form-item label="银行账户" prop="bankAccount">
          <el-input v-model="registerForm.bankAccount" placeholder="请输入16位银行卡号" class="custom-input"></el-input>
        </el-form-item>

        <el-form-item label="擅长工种" prop="skillType">
          <el-select v-model="registerForm.skillType" placeholder="请选择擅长的工种" class="custom-select" style="width: 100%">
            <el-option label="技术开发" value="技术开发"></el-option>
            <el-option label="市场营销" value="市场营销"></el-option>
            <el-option label="设计创意" value="设计创意"></el-option>
            <el-option label="文案撰写" value="文案撰写"></el-option>
            <el-option label="客户服务" value="客户服务"></el-option>
            <el-option label="数据分析" value="数据分析"></el-option>
            <el-option label="教育培训" value="教育培训"></el-option>
            <el-option label="其他" value="其他"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" placeholder="请设置4-16位字母数字组合的用户名" class="custom-input"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="请设置8-20位，且需包含字母和数字的密码" class="custom-input"></el-input>
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请再次输入密码" class="custom-input"></el-input>
        </el-form-item>

        <el-form-item prop="agreement">
          <el-checkbox v-model="registerForm.agreement" class="agreement-checkbox">
            我已阅读并同意<a href="javascript:;" class="agreement-link">《用户协议》</a>和<a href="javascript:;" class="agreement-link">《隐私政策》</a>
          </el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm" class="register-btn">注册</el-button>
        </el-form-item>
      </el-form>

      <div class="login-link">
        已有账号？<router-link to="/login" class="link">立即登录</router-link>
      </div>
    </div>

    <div class="copyright">
      ©2023 智慧零工平台 版权所有
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { registerWorker } from '../api/auth'

const router = useRouter()
const registerFormRef = ref(null)

const registerForm = reactive({
  name: '',
  phone: '',
  idCard: '',
  bankAccount: '',
  skillType: '',
  username: '',
  password: '',
  confirmPassword: '',
  agreement: false
})

const validatePass = (rule, value, callback) => {
  const reg = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,20}$/
  if (!reg.test(value)) {
    callback(new Error('密码必须包含字母和数字，长度在8-20位之间'))
  } else {
    callback()
  }
}

const validatePass2 = (rule, value, callback) => {
  if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const validatePhone = (rule, value, callback) => {
  const reg = /^1[3-9]\d{9}$/
  if (!reg.test(value)) {
    callback(new Error('请输入正确的手机号码'))
  } else {
    callback()
  }
}

const validateIdCard = (rule, value, callback) => {
  const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  if (!reg.test(value)) {
    callback(new Error('请输入正确的身份证号码'))
  } else {
    callback()
  }
}

const validateUsername = (rule, value, callback) => {
  const reg = /^[a-zA-Z0-9]{4,16}$/;
  if (!reg.test(value)) {
    callback(new Error('用户名必须为4-16位字母数字组合'))
  } else {
    callback()
  }
}

const rules = reactive({
  name: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号码', trigger: 'blur' },
    { validator: validatePhone, trigger: 'blur' }
  ],
  idCard: [
    { required: true, message: '请输入身份证号码', trigger: 'blur' },
    { validator: validateIdCard, trigger: 'blur' }
  ],
  bankAccount: [
    { required: true, message: '请输入银行卡号', trigger: 'blur' }
  ],
  skillType: [
    { required: true, message: '请选择擅长的工种', trigger: 'change' }
  ],
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { validator: validateUsername, trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { validator: validatePass, trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { validator: validatePass2, trigger: 'blur' }
  ],
  agreement: [
    { required: true, message: '请阅读并同意用户协议和隐私政策', trigger: 'change', type: 'boolean' }
  ]
})

const submitForm = () => {
  registerFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        await registerWorker(registerForm)
        ElMessage.success('注册成功，请登录')
        router.push('/login')
      } catch (error) {
        ElMessage.error(error.message || '注册失败，请稍后再试')
      }
    } else {
      return false
    }
  })
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: url('image/Register.jpg') no-repeat center center / cover;
  padding: 20px;
  position: relative;
}

.register-box {
  width: 500px;
  padding: 40px 30px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 20px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
  text-align: center;
  animation: floatIn 1s ease-out forwards;
}

@keyframes floatIn {
  0% { opacity: 0; transform: translateY(40px) scale(0.95); }
  100% { opacity: 1; transform: translateY(0) scale(1); }
}

.register-header {
  margin-bottom: 30px;
}

.logo i {
  font-size: 48px;
  color: #5dade2;
  margin-bottom: 10px;
  animation: pulse 2s infinite alternate;
}

@keyframes pulse {
  0% { transform: scale(1); opacity: 1; }
  100% { transform: scale(1.1); opacity: 0.8; }
}

.register-title {
  font-size: 28px;
  font-weight: bold;
  background: linear-gradient(90deg, #6dd5fa, #2980b9);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.register-subtitle {
  font-size: 14px;
  color: #5c5959;
  margin-top: 8px;
}

.register-form {
  margin-top: 20px;
}

.custom-input, .custom-select {
  margin-bottom: 20px;
}

.custom-input input, .custom-select :deep(.el-input__inner) {
  background: transparent !important;
  border: 1px solid rgba(255, 255, 255, 255); /* 修改为白色边框 */
  color: #fff;
  padding: 12px 15px 12px 35px;
  font-size: 16px;
  border-radius: 8px;
  height: 48px;
}

.custom-input input::placeholder, .custom-select :deep(.el-input__inner::placeholder) {
  color: #bbb;
}

.custom-input:hover input,
.custom-input:focus-within input,
.custom-select:hover :deep(.el-input__inner),
.custom-select:focus-within :deep(.el-input__inner) {
  border-color: #409EFF;
  box-shadow: 0 0 10px rgba(64, 158, 255, 0.6);
}

.custom-input :deep(.el-input__prefix), .custom-select :deep(.el-input__prefix) {
  color: #5dade2;
  left: 10px;
}

.register-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  background: linear-gradient(90deg, #409EFF, #66b1ff);
  border: none;
  border-radius: 8px;
  color: white;
  transition: all 0.3s ease;
}

.register-btn:hover {
  background: linear-gradient(90deg, #66b1ff, #409EFF);
  box-shadow: 0 0 16px rgba(64, 158, 255, 0.7);
}

.agreement-checkbox {
  color: #c0c4cc;
}

.agreement-link {
  color: #5dade2;
  text-decoration: none;
  margin: 0 5px;
}

.agreement-link:hover {
  text-decoration: underline;
}

.login-link {
  text-align: center;
  margin-top: 25px;
  font-size: 14px;
  color: #c0c4cc;
}

.link {
  color: #5dade2;
  text-decoration: none;
  margin-left: 5px;
}

.link:hover {
  text-decoration: underline;
}

.copyright {
  position: absolute;
  bottom: 20px;
  text-align: center;
  width: 100%;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.4);
}

/* 修改表单项字体颜色 */
.el-form-item__label {
  color: #c0c4cc; /* 修改为与副标题相同的颜色 */
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-box {
    width: 90%;
    padding: 30px 20px;
  }
}
</style>